﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace HaiFang
{
    [Serializable]
    public class ProxyModuleSubFieldInfo
    {
        private static string PROCEDURE_QueryModuleSubFieldInfo = "QueryModuleSubFieldInfo";
        private static string PROCEDURE_OrderModuleSubFieldInfo = "OrderModuleSubFieldInfo";
        private static string PROCEDURE_InsertModuleSubFieldInfo = "InsertModuleSubFieldInfo";

        //@CurrUserID			int = null,					--管理员编号
        //@ModuleSubID		bigint,
        //@AutoID				bigint=0					--查询所有时传0，否则传编号值
        public static IList<ModuleSubFieldInfo> QueryModuleSubFieldInfo(int CurrUserID, int ModuleSubID, int AutoID)
        {
            IList<ModuleSubFieldInfo> list = new List<ModuleSubFieldInfo>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryModuleSubFieldInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add((new SqlParameter("@ModuleSubID", ModuleSubID)));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<ModuleSubFieldInfo>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }
        //@CurrUserID			int = null,					--管理员编号
        //@ModuleSubID		bigint,
        //@UpAutoID			bigint,
        //@DownAutoID			bigint
        public static IList<ModuleSubFieldInfo> OrderModuleSubFieldInfo(int CurrUserID, int ModuleSubID, int UpAutoID, int DownAutoID)
        {
            IList<ModuleSubFieldInfo> list = new List<ModuleSubFieldInfo>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_OrderModuleSubFieldInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@ModuleSubID", ModuleSubID));
                    Parameters.Add(new SqlParameter("@UpAutoID", UpAutoID));
                    Parameters.Add(new SqlParameter("@DownAutoID", DownAutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<ModuleSubFieldInfo>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }
        //@CurrUserID			int = null,					--管理员编号
        //@UseFlag			tinyint,					--1:insert 2:update   3:delete
        //@AutoID				bigint=0,					--@UseFlag为1时传0，其它操作传编号值
        //@ModuleSubID		bigint,
        //@FieldName			varchar(200),
        //@FieldType			varchar(10),				--字符:C  数字:I  日期:D  布尔:B
        //@FieldLen			int,
        //@QueryFlag			tinyint,
        //@ResultFlag			tinyint,
        //@SpecialFlag		tinyint,	
        //@IniValue			varchar(20),
        //@BlankFlag			tinyint,	
        //@Description		varchar(128),
        //@ErrorMSG			int	output					--100：子模块存在 0：正确；其他：错误	
        public static void InsertModuleSubTable(int CurrUserID, int UseFlag, int AutoID, int ModuleSubID, string FieldName, string FieldType, int FieldLen, int QueryFlag, int ResultFlag, int SpecialFlag, string IniValue, int BlankFlag, string Description, out int ErrorMSG)
        {
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertModuleSubFieldInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@UseFlag", UseFlag));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    Parameters.Add(new SqlParameter("@ModuleSubID", ModuleSubID));
                    Parameters.Add(new SqlParameter("@FieldName", FieldName));
                    Parameters.Add(new SqlParameter("@FieldType", FieldType));
                    Parameters.Add(new SqlParameter("@FieldLen", FieldLen));
                    Parameters.Add(new SqlParameter("@QueryFlag", QueryFlag));
                    Parameters.Add(new SqlParameter("@ResultFlag", ResultFlag));
                    Parameters.Add(new SqlParameter("@SpecialFlag", SpecialFlag));
                    Parameters.Add(new SqlParameter("@IniValue", IniValue));
                    Parameters.Add(new SqlParameter("@BlankFlag", BlankFlag));
                    Parameters.Add(new SqlParameter("@Description", String.IsNullOrEmpty(Description) ? string.Empty : Description));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
    }

    [Serializable]
    public class ModuleSubFieldInfo
    {
        public int DisplayID { get; set; }
        public int AutoID { get; set; }
        public string GUID { get; set; }
        public int OrderID { get; set; }
        public int ModuleSubID { get; set; }
        public string FieldName { get; set; }
        public string FieldType { get; set; }
        public int FieldLen { get; set; }
        public int QueryFlag { get; set; }
        public int ResultFlag { get; set; }
        public int SpecialFlag { get; set; }
        public string IniValue { get; set; }
        public int BlankFlag { get; set; }
        public DateTime CreateTime { get; set; }
        public string Description { get; set; }
        public int DeleteFlag { get; set; }
    }

    [Serializable]
    public class OrderModuleSubFieldInfo
    {
        public int OrderType { get; set; }
        public ModuleSubFieldInfo IndexInfo { get; set; }
        public IList<ModuleSubFieldInfo> ModuleSubFieldInfo { get; set; }
    }

    [Serializable]
    public enum SubFieldInfoOrderType
    {
        Top = 1,
        Up = 2,
        Down = 3,
        Bottom = 4
    }
}